package com.itheima.web.servlet;

import com.itheima.util.EmailUtil;
import com.itheima.util.VerificationCodeCache;
import com.itheima.util.VerificationCodeUtil;

import java.util.Scanner;
// 这是AI给的参考用的，我的UserServlet里面写的login，register等方法，就是参考这个类来写的
public class VerificationCodeCheck {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 用户输入邮箱
//        System.out.print("请输入邮箱：");
        String email = "2505795480@qq.com";

        // 校验是否能发送验证码
        if (!VerificationCodeCache.canSendVerificationCode(email)) {
            System.out.println("验证码请求过于频繁，请稍后再试！");
            return;
        }

        // 生成验证码
        String verificationCode = VerificationCodeUtil.generateCode();
        // 保存验证码
        VerificationCodeCache.saveVerificationCode(email, verificationCode);

        // 发送验证码到邮箱
        if (EmailUtil.sendVerificationCode(email, verificationCode)) {
            System.out.println("验证码已发送，请查收邮件！");
        }

        // 用户输入验证码
        System.out.print("请输入收到的验证码：");
        String userInputCode = scanner.nextLine();

        // 校验验证码
        if (userInputCode.equals(VerificationCodeCache.getVerificationCode(email))) {
            System.out.println("验证码验证成功！");
        } else {
            System.out.println("验证码错误！");
        }
    }
}
